/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.close.basic.controller;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.close.basic.dto.ClosedGeneralCarAuditDTO;
import com.hopes.bizz.close.basic.dto.ClosedGeneralCarDTO;
import com.hopes.bizz.close.basic.vo.ClosedGeneralCarExcelCompanyVO;
import com.hopes.bizz.close.common.constant.BasicDicConstant;
import com.hopes.bizz.close.common.constant.CloseModuleEnums;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.log.annotation.SysLog;
import com.hopes.bizz.close.basic.entity.ClosedGeneralCarEntity;
import com.hopes.bizz.close.basic.dto.ClosedGeneralCarExcelDTO;
import com.hopes.bizz.close.basic.vo.ClosedGeneralCarExcelVO;
import com.hopes.bizz.close.basic.service.ClosedGeneralCarService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 普通车辆信息
 *
 * @author pig
 * @date 2023-10-23 17:28:41
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/generalcar")
@Tag(description = "generalcar", name = "普通车辆信息管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ClosedGeneralCarController {

    private final ClosedGeneralCarService closedGeneralCarService;

    /**
     * 分页查询
     *
     * @param page             分页对象
     * @param closedGeneralCarDTO 普通车辆信息
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('basic_generalcar_view')")
    public R getClosedGeneralCarPage(Page page, ClosedGeneralCarDTO closedGeneralCarDTO) {
        LambdaQueryWrapper<ClosedGeneralCarEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotBlank(closedGeneralCarDTO.getType()), ClosedGeneralCarEntity::getType, closedGeneralCarDTO.getType());
        wrapper.eq(StrUtil.isNotBlank(closedGeneralCarDTO.getType()) && closedGeneralCarDTO.getType().equals(CloseModuleEnums.ParkCompanyTypeEnum.company.name())
                && ObjUtil.isNotNull(closedGeneralCarDTO.getOrgId()), ClosedGeneralCarEntity::getOrgId, closedGeneralCarDTO.getOrgId());
        wrapper.eq(ObjUtil.isNotNull(closedGeneralCarDTO.getCarType()), ClosedGeneralCarEntity::getCarType, closedGeneralCarDTO.getCarType());
        wrapper.eq(ObjUtil.isNotNull(closedGeneralCarDTO.getPassType()), ClosedGeneralCarEntity::getPassType, closedGeneralCarDTO.getPassType());
        wrapper.eq(StrUtil.isNotBlank(closedGeneralCarDTO.getUseFlag()), ClosedGeneralCarEntity::getUseFlag, closedGeneralCarDTO.getUseFlag());
        wrapper.eq(ObjUtil.isNotNull(closedGeneralCarDTO.getAuditStatus()), ClosedGeneralCarEntity::getAuditStatus, closedGeneralCarDTO.getAuditStatus());
        wrapper.like(StrUtil.isNotBlank(closedGeneralCarDTO.getKeyword()), ClosedGeneralCarEntity::getLicencePlateNumber, closedGeneralCarDTO.getKeyword())
                .or().like(StrUtil.isNotBlank(closedGeneralCarDTO.getKeyword()), ClosedGeneralCarEntity::getPhone, closedGeneralCarDTO.getKeyword())
                .or().like(StrUtil.isNotBlank(closedGeneralCarDTO.getKeyword()), ClosedGeneralCarEntity::getCarOwner, closedGeneralCarDTO.getKeyword());
        return R.ok(closedGeneralCarService.page(page, wrapper));
    }

    /**
     * 分页查询 （移动端）我的访客申请
     * @param page 分页对象
     * @param closedGeneralCarDTO 车辆进出申请
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/selfPage" )
    @PreAuthorize("@pms.hasPermission('visitor_apply_view')" )
    public R getAppGeneralCarSelfPage(Page page, ClosedGeneralCarDTO closedGeneralCarDTO) {
        return R.ok(closedGeneralCarService.getAppGeneralCarSelfPage(page, closedGeneralCarDTO));
    }

    /**
     * 分页查询 （移动端）企业待审批的申请
     * @param page 分页对象
     * @param closedGeneralCarDTO 车辆进出申请
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/audit/toDoPage" )
    @PreAuthorize("@pms.hasPermission('visitor_apply_view')" )
    public R getAppGeneralCarAuditToDoPage(Page page, ClosedGeneralCarDTO closedGeneralCarDTO) {
        final List<Integer> auditStatusList = Collections.singletonList(BasicDicConstant.CLOSE_VISITOR_AUDIT_STATUS_NOT_AUDIT);
        return R.ok(closedGeneralCarService.queryAppGeneralCarAuditPage(page, auditStatusList, closedGeneralCarDTO));
    }

    /**
     * 分页查询 （移动端）企业审批完成的申请
     * @param page 分页对象
     * @param closedGeneralCarDTO 车辆进出申请
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/audit/donePage" )
    @PreAuthorize("@pms.hasPermission('visitor_apply_view')" )
    public R getAppGeneralCarAuditDonePage(Page page, ClosedGeneralCarDTO closedGeneralCarDTO) {
        final List<Integer> auditStatusList = Arrays.asList(
                BasicDicConstant.CLOSE_VISITOR_AUDIT_STATUS_PASS_AUDIT,
                BasicDicConstant.CLOSE_VISITOR_AUDIT_STATUS_NOT_PASS_AUDIT);
        return R.ok(closedGeneralCarService.queryAppGeneralCarAuditPage(page, auditStatusList, closedGeneralCarDTO));
    }

    /**
     * 通过id查询普通车辆信息
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('basic_generalcar_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(closedGeneralCarService.getVoById(id));
    }

    /**
     * 新增普通车辆信息
     *
     * @param closedGeneralCar 普通车辆信息
     * @return R
     */
    @Operation(summary = "新增普通车辆信息", description = "新增普通车辆信息")
    @SysLog("新增普通车辆信息")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('basic_generalcar_add')")
    public R save(@RequestBody ClosedGeneralCarEntity closedGeneralCar) {
        return R.ok(closedGeneralCarService.save(closedGeneralCar));
    }


    /**
     * 新增并提交普通车辆信息
     *
     * @param closedGeneralCar 普通车辆信息
     * @return R
     */
    @Operation(summary = "新增并提交普通车辆信息", description = "新增并提交普通车辆信息")
    @SysLog("新增并提交普通车辆信息")
    @PostMapping("/saveAndSubmit")
    @PreAuthorize("@pms.hasPermission('basic_generalcar_add')")
    public R saveAndSubmit(@RequestBody ClosedGeneralCarEntity closedGeneralCar) {
        return R.ok(closedGeneralCarService.saveAndSubmit(closedGeneralCar));
    }


    /**
     * 修改普通车辆信息
     *
     * @param closedGeneralCar 普通车辆信息
     * @return R
     */
    @Operation(summary = "修改普通车辆信息", description = "修改普通车辆信息")
    @SysLog("修改普通车辆信息")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('basic_generalcar_edit')")
    public R updateById(@RequestBody ClosedGeneralCarEntity closedGeneralCar) {
        return R.ok(closedGeneralCarService.updateById(closedGeneralCar));
    }

    /**
     * 提交普通车辆信息
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "审核普通车辆信息", description = "审核普通车辆信息")
    @SysLog("审核普通车辆信息")
    @GetMapping("/submit/{id}")
    @PreAuthorize("@pms.hasPermission('basic_generalcar_edit')")
    public R submitById(@PathVariable("id") Long id) {
        return R.ok(closedGeneralCarService.submitById(id));
    }

    /**
     * 审核普通车辆信息
     *
     * @param closedGeneralCarAuditDTO 审核普通车辆信息
     * @return R
     */
    @Operation(summary = "审核普通车辆信息", description = "审核普通车辆信息")
    @SysLog("审核普通车辆信息")
    @PutMapping("/audit")
    @PreAuthorize("@pms.hasPermission('basic_generalcar_audit')")
    public R auditById(@RequestBody ClosedGeneralCarAuditDTO closedGeneralCarAuditDTO) {
        return R.ok(closedGeneralCarService.auditById(closedGeneralCarAuditDTO));
    }


    /**
     * 通过id删除普通车辆信息
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除普通车辆信息", description = "通过id删除普通车辆信息")
    @SysLog("通过id删除普通车辆信息")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('basic_generalcar_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(closedGeneralCarService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template/{type}")
    @PreAuthorize("@pms.hasPermission('basic_generalcar_export')")
    public List<ClosedGeneralCarExcelVO> template(@PathVariable("type") CloseModuleEnums.ParkCompanyTypeEnum parkCompanyTypeEnum) {
        if (CloseModuleEnums.ParkCompanyTypeEnum.company.equals(parkCompanyTypeEnum)) {
            return Collections.singletonList(new ClosedGeneralCarExcelCompanyVO());
        }
        return closedGeneralCarService.exportExcel(Collections.singletonList(new ClosedGeneralCarEntity()));
    }

    /**
     * 导出excel 表格
     *
     * @param closedGeneralCarDTO 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('basic_generalcar_export')")
    public List<?> export(ClosedGeneralCarDTO closedGeneralCarDTO) {
        final LambdaQueryWrapper<ClosedGeneralCarEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ClosedGeneralCarEntity::getType, closedGeneralCarDTO.getType());
        wrapper.eq(StrUtil.isNotBlank(closedGeneralCarDTO.getType()) && closedGeneralCarDTO.getType().equals(CloseModuleEnums.ParkCompanyTypeEnum.company.name())
                && ObjUtil.isNotNull(closedGeneralCarDTO.getOrgId()), ClosedGeneralCarEntity::getOrgId, closedGeneralCarDTO.getOrgId());
        wrapper.eq(ObjUtil.isNotNull(closedGeneralCarDTO.getCarType()), ClosedGeneralCarEntity::getCarType, closedGeneralCarDTO.getCarType());
        wrapper.eq(ObjUtil.isNotNull(closedGeneralCarDTO.getPassType()), ClosedGeneralCarEntity::getPassType, closedGeneralCarDTO.getPassType());
        wrapper.eq(StrUtil.isNotBlank(closedGeneralCarDTO.getUseFlag()), ClosedGeneralCarEntity::getUseFlag, closedGeneralCarDTO.getUseFlag());
        wrapper.eq(ObjUtil.isNotNull(closedGeneralCarDTO.getAuditStatus()), ClosedGeneralCarEntity::getAuditStatus, closedGeneralCarDTO.getAuditStatus());
        wrapper.like(StrUtil.isNotBlank(closedGeneralCarDTO.getKeyword()), ClosedGeneralCarEntity::getLicencePlateNumber, closedGeneralCarDTO.getKeyword())
                .or().like(StrUtil.isNotBlank(closedGeneralCarDTO.getKeyword()), ClosedGeneralCarEntity::getPhone, closedGeneralCarDTO.getKeyword())
                .or().like(StrUtil.isNotBlank(closedGeneralCarDTO.getKeyword()), ClosedGeneralCarEntity::getCarOwner, closedGeneralCarDTO.getKeyword());
        final Class<? extends ClosedGeneralCarExcelVO> clazz = CloseModuleEnums.ParkCompanyTypeEnum.park.name().equals(closedGeneralCarDTO.getType()) ? ClosedGeneralCarExcelVO.class : ClosedGeneralCarExcelCompanyVO.class;
        return closedGeneralCarService.exportExcelWithClass(closedGeneralCarService.list(wrapper), clazz);
    }

    /**
     * 导入excel 表格
     *
     * @param closedGeneralCarExcelDtoList 查询条件
     * @param bindingResult                错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('basic_generalcar_export')")
    public R importExcel(@RequestExcel List<ClosedGeneralCarExcelDTO> closedGeneralCarExcelDtoList, BindingResult bindingResult) {
        return closedGeneralCarService.importExcel(closedGeneralCarExcelDtoList, bindingResult);
    }
}
